﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Base.Enmu;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System;
using UnityEngine.Events;
using DG.Tweening;
public class EventTriggerListener : EventTrigger
{

    public EventButon eventbut;
    public buttonStyle butSytle;
    public buttonStatus status;
    public Color NormalColor=new Color32(255,255,255,255);
    public Color EnterColor=new Color32(159,159,159,255);
    public Color PressedColor=new Color32(118, 118, 118, 255);

    public Sprite NormalSprite;
    public Sprite EnterSprite;
    public Sprite PressedSprite;

    public Vector2 NormalScale;
    public Vector2 EnterScale;
    public Vector2 PressedScale;

    public Vector3 EnterX;
    public Vector3 ComeX;

    public PluralButton plural;
    public delegate void VoidDelegate();
    public delegate void BoolDelegate(bool state);
    public delegate void FloatDelegate(float delta);
    public delegate void VectorDelegate(Vector2 delta);
    public delegate void ObjectDelegate(GameObject obj);
    public delegate void KeyCodeDelegate(KeyCode key);

    public VoidDelegate onClick;
    public VoidDelegate onDown;
    public VoidDelegate onEnter;
    public VoidDelegate onExit;
    public VoidDelegate onUp;
    public VoidDelegate onSelect;
    public VoidDelegate onUpdateSelect;
    public void Awake()
    {
    }
    //static public EventTriggerListener Get(Transform transform)
    //{
    //    EventTriggerListener listener = transform.GetComponent<EventTriggerListener>();
    //    if (listener == null)
    //        listener = transform.gameObject.AddComponent<EventTriggerListener>();
    //    return listener;
    //}
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null)
            onClick();
        Image image = GetComponent<Image>();
        switch (eventbut)
        {
            case EventButon.singleEvent:
                switch (butSytle)
                {
                    case buttonStyle.color:
                        image.color = PressedColor;
                        break;
                    case buttonStyle.sprite:
                        image.sprite = PressedSprite;
                        break;
                    case buttonStyle.scale:
                        image.rectTransform.sizeDelta = PressedScale;
                        break;
                }
                break;
            case EventButon.pluralEvent:
                switch (butSytle)
                {
                    case buttonStyle.color:
                        image.color = PressedColor;
                        break;
                    case buttonStyle.sprite:
                        image.sprite = PressedSprite;
                        break;
                    case buttonStyle.scale:
                        image.rectTransform.sizeDelta = PressedScale;
                        break;
                }
                if (plural != null)
                    plural.call_back_OnClick += CallBack;
                break;
        }
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null)
            onDown();
        Image image = GetComponent<Image>();
        switch (eventbut)
        {
            case EventButon.singleEvent:
                switch (butSytle)
                {
                    case buttonStyle.color:
                        image.color = PressedColor;
                        break;
                    case buttonStyle.sprite:
                        image.sprite = PressedSprite;
                        break;
                    case buttonStyle.scale:
                        image.rectTransform.sizeDelta = PressedScale;
                        break;
                }
                break;
            case EventButon.pluralEvent:
                switch (butSytle)
                {
                    case buttonStyle.color:
                        image.color = PressedColor;
                        break;
                    case buttonStyle.sprite:
                        image.sprite = PressedSprite;
                        break;
                    case buttonStyle.scale:
                        image.rectTransform.sizeDelta = PressedScale;
                        break;
                }
                if (plural != null)
                    plural.call_back_OnClick += CallBack;
                break;
        }

    }

    public override void OnPointerEnter(PointerEventData eventData)
    {
                if (onEnter != null)
                    onEnter();
                Image image = GetComponent<Image>();
        switch (eventbut)
        {
            case EventButon.singleEvent:
                switch (butSytle)
                {
                    case buttonStyle.color:
                        image.color = EnterColor;
                        break;
                    case buttonStyle.sprite:
                        image.sprite = EnterSprite;
                        break;
                    case buttonStyle.scale:
                        image.rectTransform.sizeDelta = EnterScale;
                        break;
                    case buttonStyle.animation:
                        break;
                }
                break;
            case EventButon.pluralEvent:
                switch (butSytle)
                {
                    case buttonStyle.color:
                        image.color = EnterColor;
                        break;
                    case buttonStyle.sprite:
                        image.sprite = EnterSprite;
                        break;
                    case buttonStyle.scale:
                        image.rectTransform.sizeDelta = EnterScale;
                        break;
                    case buttonStyle.animation:
                        //不要操作
                        break;
                }
                if (plural != null)
                    plural.call_back_OnClick += CallBack;
                break;
        }
   
    }

    public override void OnPointerExit(PointerEventData eventData)
    {
                if (onExit != null)
                    onExit();
                Image image = GetComponent<Image>();
                switch (eventbut)
                {
                    case EventButon.singleEvent:
                        switch (butSytle)
                        {
                            case buttonStyle.color:
                                image.color = NormalColor;
                                break;
                            case buttonStyle.sprite:
                                image.sprite = NormalSprite;
                                break;
                            case buttonStyle.scale:
                                image.rectTransform.sizeDelta = NormalScale;
                                break;
                        }
                        break;
                    case EventButon.pluralEvent:
                        switch (butSytle)
                        {
                            case buttonStyle.color:
                                image.color = NormalColor;
                                break;
                            case buttonStyle.sprite:
                                image.sprite = NormalSprite;
                                break;
                            case buttonStyle.scale:
                                image.rectTransform.sizeDelta = NormalScale;
                                break;
                        }
                        if (plural != null)
                            plural.call_back_OnClick += CallBack;
                        break;
                }
        
        
    }

    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null)
            onUp();
    }

    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null)
            onSelect();
    }

    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null)
            onUpdateSelect();
    }
    /// <summary>
    /// 复选效果回调
    /// </summary>
    /// <param name="tab"></param>
    private void CallBack(EventTriggerListener tab)
    {
        Image image = tab.GetComponent<Image>();
        switch (butSytle)
        {
            case buttonStyle.color:
                image.color = tab.Equals(this) ? PressedColor : NormalColor;
                break;
            case buttonStyle.sprite:
                image.sprite = tab.Equals(this) ? PressedSprite : NormalSprite;
                break;
            case buttonStyle.scale:
                image.rectTransform.sizeDelta= tab.Equals(this) ? PressedScale : NormalScale;
                break;
            case buttonStyle.animation:
                if (!tab.Equals(this))
                {
                    image.transform.DOMove(tab.Equals(this)?EnterX:ComeX,1);
                }
                break;
        }
    }
    private void OnDisable()
    {
        if (plural != null)
            plural.call_back_OnClick -= CallBack;
    }

    public void DoMoveX(bool enter=true)
    {
        transform.DOMove(enter ? EnterX : ComeX, 1);
    }

    [Serializable]
    public class TimeEventTrigger
    {
        [Serializable]
        public class TimeEvent : UnityEvent
        {

        }
        [SerializeField]
        public TimeEvent m_TimeEvent = new TimeEvent();
        
    }
}
